//
//  AddressBookProgressView.swift
//  EasePods
//
//  Created by 易先生 on 2024/5/20.
//

import UIKit

enum AddressBookpgradeState {
    
    case starting,uploading, completed, completedAndTips, failed
}


class AddressBookProgressView: View {

    override func makeUI() {
        super.makeUI()
        
        self.addSubview(stackView)

        stackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-80)
        }
        
        self.updateState(state: .starting)
    }
    
    public func updateState(state: AddressBookpgradeState) {
        stackView.removeAllSubViews()
        stackView.spacing = 0
        switch state {
        case .starting:
            stackView.addArrangedSubview(completeBookButton)
            completeBookButton.setTitle(R.string.localizable.common_confirm(), for: .normal)
        case .uploading:
            
            tipsLabel.text = R.string.localizable.setting_contact_uploading()
            tipsLabel.font = Fonts.systemFont(ofSize: 12)
            tipsLabel.textColor = R.color.common_3_text()
            stackView.spacing = 14
            stackView.addArrangedSubview(tipsLabel)
            stackView.addArrangedSubview(progressView)
            progressView.snp.makeConstraints { make in
                make.width.equalToSuperview()
                make.height.equalTo(6)
            }
        case .completed:
            tipsLabel.text = R.string.localizable.setting_contact_upload_hint()
            tipsLabel.textColor = R.color.common_23_text_second()
            tipsLabel.font = Fonts.systemFont(ofSize: 14)
            stackView.addArrangedSubview(tipsLabel)
        case .completedAndTips:
            self.makeToast(R.string.localizable.setting_contact_upload_success(), duration: 2, point: CGPoint(x: self.centerX, y: tipsLabel.top-60), title: nil, image: nil, completion: nil)
            tipsLabel.text = R.string.localizable.setting_contact_upload_hint()
            tipsLabel.textColor = R.color.common_23_text_second()
            tipsLabel.font = Fonts.systemFont(ofSize: 14)
            stackView.addArrangedSubview(tipsLabel)
        case .failed:
            
            completeBookButton.setTitle(R.string.localizable.setting_contact_upload_fail(), for: .normal)

            stackView.addArrangedSubview(completeBookButton)
        }
    }
    
    public func updateProgress(progress: Float) {
        progressView.progress = progress
    }
    
    private lazy var stackView: StackView = {
        let view = StackView()
        view.alignment = .center
        view.axis = .vertical
        return view
    }()
    
    public lazy var completeBookButton: Button = {
        let button = Button()
        button.setTitleColor(R.color.common_24_button_text(), for: .normal)
        button.setTitle(R.string.localizable.common_confirm(), for: .normal)
        button.titleLabel?.font = Fonts.systemFont(ofSize: 16)
        button.layer.cornerRadius = 6
        button.contentEdgeInsets = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
        button.setBackgroundImage(UIImage.imageWithColor(R.color.common_22_button_n() ?? .clear), for: .normal)
        button.setBackgroundImage(UIImage.imageWithColor(R.color.common_33_button_disable() ?? .clear), for: .disabled)
        button.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: kScreenWidth-40, height: 57))
        }
        return button
    }()
    
    private lazy var tipsLabel: Label = {
        let label = Label()
        label.textColor = R.color.common_3_text()
        label.text = R.string.localizable.setting_contact_uploading()
        label.font = Fonts.systemFont(ofSize: 12)
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    private lazy var progressView: UIProgressView = {
        let progressView = UIProgressView()
        progressView.progressViewStyle = .bar
        progressView.progress = 0.0
        progressView.progressTintColor = R.color.common_22_button_n()
        progressView.trackTintColor = R.color.common_17_bg()
        return progressView
    }()

}
